package com.yswee.mallnavi.view.food.grid;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.yswee.framework.utils.ViewInject;
import com.yswee.framework.widget.layoutview.MRelativeLayout;
import com.yswee.mallnavi.R;
import com.yswee.mallnavi.entity.info.InfoEntity;
import com.yswee.mallnavi.view.food.IGridView;


public class GridItem extends MRelativeLayout<InfoEntity> {

	@ViewInject
	private TextView tvname;
	@ViewInject
	private ImageView imgpicture;

	private IGridView.IActionCallback mActionCallback;

	public void setActionCallback(IGridView.IActionCallback c) {
		mActionCallback = c;
	}
	
	public GridItem(Context context) {
		super(context);
	}
	
	public GridItem(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected int getLayoutResId() {
		return R.layout.view_info_food_griditem;
	}
	
	@Override
	protected void onBindListener() {
		super.onBindListener();
		OnClickListener onclick = new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mActionCallback != null) {
					mActionCallback.onInfoClicked(v, mDataItem);
				}
			}
		};
		setOnClickListener(onclick);
	}

	@Override
	protected void onApplyData() {
		tvname.setText(mDataItem.name);
		imgpicture.setBackgroundResource(R.drawable.default_image_x);
		Glide.with(this)
				.load(mDataItem.pictureUrl)
				.apply(new RequestOptions().transform(new RoundedCorners((int) mContext.getResources().getDimension(R.dimen.dp_10))))
				.addListener(new RequestListener<Drawable>() {
					@Override
					public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
						return false;
					}

					@Override
					public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
						imgpicture.setBackgroundResource(R.color.transparent);
						return false;
					}
				})
				.into(imgpicture);
	}

}
